using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Auto.Data.Setting;
using Auto.Components;
using Auto.Web.Common;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace BaseSystem._System
{
    public partial class UserSetting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {
            txtSessionTimeout.Text = GetSettingValue("SessionTime");
            ddlMenuStyle.SelectedValue = GetSettingValue("MenuStyle");
            ddlLanguage.SelectedValue = GetSettingValue("CultureInfo");
            txtPageSize.Text = GetSettingValue("PageSize");
            ddlDateFormat.SelectedValue = GetSettingValue("DateFormat");
        }

        private string GetSettingValue(string settingID)
        {
            try
            {
                UserSettings us = new UserSettings(SessionBox.User.UserID, settingID);
                if (!string.IsNullOrEmpty(us.SettingValue))
                {
                    return us.SettingValue;
                }
                else
                {
                    Settings st = new Settings(SessionBox.CurrentSystem, settingID);
                    return st.SettingValue;
                }
            }
            catch (Exception ex)
            {
                Jscript.AjaxAlert(this,ex.Message);
                return "";
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            string strSessionTime = txtSessionTimeout.Text.Trim();
            string strPageSize = txtPageSize.Text.Trim();
            string sessiontimeregex = @"^([1-9]|[1-2][0-9]|30)$";
            string pagesizeregex= @"^([1-9]|[1-9][0-9]|1[0-9]{1,2}|200)$";
            bool va = true;
            if (!Regex.IsMatch(strSessionTime, sessiontimeregex))
            {
                Jscript.AjaxAlert(this, "Session time must be integer between 1 and 30");
                va = false;
            }
            if (!Regex.IsMatch(strPageSize, pagesizeregex))
            {
                Jscript.AjaxAlert(this, "Page size must be integer between 1 and 200");
                va = false;
            }
            if (va)
            {
                UpdateSettingValue("SessionTime", strSessionTime);
                UpdateSettingValue("MenuStyle", ddlMenuStyle.SelectedValue);
                UpdateSettingValue("CultureInfo", ddlLanguage.SelectedValue);
                UpdateSettingValue("PageSize", strPageSize);
                UpdateSettingValue("DateFormat", ddlDateFormat.SelectedValue);
                Jscript.AjaxAlert(this,"Settings update OK");
            }
        }

        private void UpdateSettingValue(string settingID,string settingValue)
        {
            try
            {
                UserSettings us = new UserSettings(SessionBox.User.UserID, settingID);
                us.SettingValue = settingValue;
                if (!us.IsNew)
                {
                    us.Update();
                }
                else
                {
                    us.Save();
                }
            }
            catch (Exception ex)
            {
                Jscript.AjaxAlert(this,ex.Message);
            }
        }
    }
}
